<template>
	<view style="width: 100%;">
		<view class="select f-row-between " @click="onSelect">
			<label>{{ tmpAddress.branch_name }}</label>
			<label class="icons iconxiajiantou"></label>
		</view>
		<view class="addressPopup-box">
			<uni-popup id="addressPopup" ref="addressPopup" :type="'bottom'" @change="change">
				<picker-view class=" f-row-Wcenter" :indicator-style="indicatorStyle" v-if="visible" :value="addressvalue" @change="addressChange">
					<picker-view-column style="width: 900upx;" class="picker-view">
						<view class="item f-center" v-for="(item, index) in addresslist" :key="index">{{ item.branch_name }}</view>
					</picker-view-column>
				</picker-view>
				<view style="width: 100%;" class="btn-Block f-center">
					<view class="btn-left f-center" @click="onAddressCancel">取消</view>
					<view class="btn-right f-center" @click="onAddressSubmit">确定</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import address from './addresslist.js';
	export default {
		components: {},
		data() {
			return {
				tmpAddress: {
					branch_name: '机构总部',
					branch_no: '',
					branch_status: 1
				},

				visible: true,
				indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth / (750 / 100))}px;`,
				addresslist: [{}],
				addressvalue: [0]
			};
		},
		created() {
			
			if (address.list.length < 1) {
				this.$api_server.inc_GetAllBranch({
					data: {
						version: '1',
						pageNo: 0,
						onePageNum: 10,
						Params: {
							branch_status: 1
						}
					},
					success: res => {

						if (res.data) {
							let data = [{
								branch_jg: '%', //string 机构编号
								branch_name: '全部', //string 机构名称
								branch_no: '%',
							}];
							this.addresslist = [];
							this.addresslist = res.data.data;
							this.addresslist = [...data, ...this.addresslist];
							if(this.addresslist.length > 0){
								address.list = this.addresslist;
								this.addresslist.forEach((item, index) => {
									if (item.branch_name == this.userInfo.branch_name) {
										this.$nextTick(function() {
											this.addressvalue[0] = index;
											this.tmpAddress = this.addresslist[this.addressvalue[0]];
											this.$emit('onSubmitAddress', {
												address: this.addresslist[this.addressvalue[0]],
												login: true
											});
										})
									}
								})
							}
							
						}
					},
					fail(error) {
						console.log('error', error);
					}
				});
			}else{
				this.addresslist = address.list;
				this.addresslist.forEach((item, index) => {
					if (item.branch_name == this.userInfo.branch_name) {
						this.$nextTick(function() {
							this.addressvalue[0] = index;
							this.tmpAddress = this.addresslist[this.addressvalue[0]];
							this.$emit('onSubmitAddress', {
								address: this.addresslist[this.addressvalue[0]],
								login: true
							});
						})
					}
				})
			}
		},
		computed: {
			userInfo() {
				var userInfo = this.$store.getters.getUserInfo;

				// this.addresslist.forEach(item =>{
				// 	if(item.branch_name == userInfo){

				// 	}
				// })
				return userInfo;
			}
		},
		methods: {
			//头部下拉
			onSelect() {
				this.$refs.addressPopup.open();
			},
			change(e) {},
			//头部下拉按钮
			onAddressCancel() {
				this.$refs.addressPopup.close();
			},
			onAddressSubmit() {
				this.tmpAddress = this.addresslist[this.addressvalue];
				this.$emit('onSubmitAddress', {
					address: this.addresslist[this.addressvalue],
					login: true
				});
				this.$refs.addressPopup.close();
			},
			addressChange(e) {
				this.addressvalue = e.detail.value;
			}
		}
	};
</script>
